#pragma once 

#include <iostream>
#include <fstream>
#include <unordered_map>
#include <string>
#include "log.hpp"


const std::string dictname = "./dict.txt";
const std::string sep = ":";

bool Split(std::string& str, std::string*  part1, std::string* part2)
{
    auto pos = str.find(sep);
    if(pos == std::string::npos) return false;
    *part1 = str.substr(0, pos);
    *part2 = str.substr(pos + 1);

    return true;
}
class Init
{
public:
    Init()
    {
        std::ifstream in(dictname);
        if(!in.is_open())
        {
            LOG(FATAL, "打开文件失败");
            exit(1);
        }
        std::string line;
        while(std::getline(in, line))
        {
            std::string part1, part2;
            Split(line, &part1, &part2);
            m_dict.insert({part1, part2});
        }

        // auto iter = m_dict.begin();
        // while(iter != m_dict.end())
        // {
        //     std::cout << iter->first << ":" << iter->second << std::endl;
        //     ++iter;
        // }
        in.close();     
    }

    std::string translation(const std::string& key)
    {
        auto iter = m_dict.find(key);
        if(iter == m_dict.end()) return "UnKnow";
        else return iter->second;
    }
private:
    std::unordered_map<std::string, std::string> m_dict;
};